package com.dlxj.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dlxj.dao.BaseDAO;
import com.dlxj.entity.Bugtype;
import com.dlxj.entity.Circuitry;
import com.dlxj.entity.CircuitryPole;
import com.dlxj.entity.Patroltask;
import com.dlxj.entity.Pole;
import com.dlxj.entity.Receiptpatrol;
import com.dlxj.entity.User;
import com.dlxj.service.PoleService;
import com.dlxj.util.ConstClass;
import com.dlxj.util.PageBean;

@SuppressWarnings("unchecked")
public class PoleServiceImpl implements PoleService {

	@SuppressWarnings("rawtypes")
	BaseDAO baseDAO;

	@Override
	public int addPole(Pole pole) {
		int count = baseDAO.countHql("select count(*) from Pole where id = ?",
				new Object[] { pole.getId() });
		if (count > 0) {
			return ConstClass.ADD_FAILURE;
		}
		baseDAO.save(pole);
		return ConstClass.ADD_SUCCESS;
	}

	@SuppressWarnings("rawtypes")
	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	@SuppressWarnings("rawtypes")
	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	@Override
	public List<Pole> findPoleList() {
		return (List<Pole>) baseDAO.findByHql("from Pole");
	}

	@Override
	public int delPoleById(String id) {

		if (baseDAO.findByHql("from CircuitryPole where pid =?",
				new Object[] { id }).size() == 0
				&& baseDAO
						.findByHql(
								"from Circuitry where poleByEndpoleNo.id = ? or poleByStartpoleNo.id = ?",
								new Object[] { id, id }).size() == 0) {

			baseDAO.delete(Pole.class, id);
			return ConstClass.DELETE_SUCCESS;
		}
		return ConstClass.DELETE_FAILURE;
	}

	@Override
	public Pole findPoleById(String id) {

		return (Pole) baseDAO.findById(Pole.class, id);
	}

	@Override
	public List<Pole> findPoliesByStartPoleIdAndEndStartPoleIdAndCircuitryId(
			String startPoleId, String endPoleId, String cid) {
		List<Pole> list = new ArrayList<Pole>();
		List<CircuitryPole> cps = (List<CircuitryPole>) baseDAO.findByHql(
				"from CircuitryPole where pid=? and cid=?", new Object[] {
						startPoleId, cid });
		int spos = -1;
		if (cps.size() == 1) {
			spos = cps.get(0).getPosition();
		}
		cps = (List<CircuitryPole>) baseDAO.findByHql(
				"from CircuitryPole where pid=? and cid=?", new Object[] {
						endPoleId, cid });
		int epos = -1;
		if (cps.size() == 1) {
			epos = cps.get(0).getPosition();
		}
		cps = (List<CircuitryPole>) baseDAO
				.findByHql(
						"from CircuitryPole where cid=? and position >= ? and position <= ? order by position asc",
						new Object[] { cid, spos, epos });
		for (CircuitryPole cp : cps) {
			list.add((Pole) baseDAO.findById(Pole.class, cp.getPid()));
		}
		return list;
	}

	@Override
	public List<Receiptpatrol> findHasBug() {
		List<Receiptpatrol> receiptpatrols = baseDAO
				.findByHql(
						"from Receiptpatrol where ptid in(select id from Patroltask where state =? ) and ptid not in (select ptid from DebugtaskPole)",
						new Object[] { ConstClass.TASK_STATE_SURE });
		for (Receiptpatrol receiptpatrol : receiptpatrols) {
			Pole pole = (Pole) baseDAO.findById(Pole.class,
					receiptpatrol.getPid());
			receiptpatrol.setPole(pole);
			User u = (User) baseDAO
					.findById(User.class, receiptpatrol.getUid());
			receiptpatrol.setUserName(u.getName());
			Patroltask patroltask = (Patroltask) baseDAO.findById(
					Patroltask.class, receiptpatrol.getPtid());
			Circuitry circuitry = (Circuitry) baseDAO.findById(Circuitry.class,
					patroltask.getCid());
			receiptpatrol.setRouteName(circuitry.getName());
			Bugtype bt = (Bugtype) baseDAO.findById(Bugtype.class,
					receiptpatrol.getBid());
			receiptpatrol.setTypeName(bt.getName());
		}
		return receiptpatrols;
	}

	@Override
	public int update(Pole pole) {
//		Pole p = (Pole) baseDAO.findById(Pole.class, pole.getId());
//		p.setId(pole.getId());
		int success=0;
		 String judge=pole.getUpId(); 
		 List<Circuitry> circuitry=new ArrayList<Circuitry>();
		 List<CircuitryPole> circuitryPole = new ArrayList<CircuitryPole>();
		 circuitry = (List<Circuitry>) baseDAO.findByHql(
					"from Circuitry where startpoleNo=?  or  endpoleNo=?  ",
					new Object[] { pole.getId(),pole.getId() });
		 
		 circuitryPole = (List<CircuitryPole>) baseDAO.findByHql(
					"from CircuitryPole where pid=?  ",
					new Object[] { pole.getId() });
		 
		 if(circuitry.size()!=0|circuitryPole.size()!=0){
			 return success=3;
		 }
		try {
			if(judge==null){
				baseDAO.update(pole);
			}else{
				
				Pole p=(Pole)baseDAO.findById(Pole.class, pole.getId());
				baseDAO.delete(Pole.class, p.getId());
				Pole u=new Pole();
				u.setId(pole.getUpId());
				u.setLng(pole.getLng());
				u.setLat(pole.getLat());
				u.setState(pole.getState());
				baseDAO.save(u);
			}
			success=1;
		} catch (Exception e) {
			e.printStackTrace();
		}
//		Pole sessionPole = (Pole) ServletActionContext.getRequest()
//				.getSession().getAttribute("pole");
//		sessionPole.setId(pole.getId());
//		ServletActionContext.getRequest().getSession()
//				.setAttribute("pole", sessionPole);
		return success;

	}



	/**
	 * 分页查询
	 */
	public PageBean findPoleSort(int pageIndex, int pageSize) {
		String hql = "from Pole ";
		PageBean bean = baseDAO.findPageBeanHQL(hql, pageIndex, pageSize,
				null);
		return bean;
	}

	@Override
	public PageBean showPole(Pole pole, int pageIndex, int pageSize) {
		String hql = "from Pole ";
		String id="";
		PageBean bean;
		if(pole!=null){
			id=pole.getId();
			hql+=" where id =? ";
			bean = baseDAO.findPageBeanHQL(hql, pageIndex, pageSize,
					new Object[]{id}); 
		}else{
			bean = baseDAO.findPageBeanHQL(hql, pageIndex, pageSize,
					new Object[]{}); 
		}
		
		return bean;
	}


}
